package com.inspur.edp.common.expr.visitor.array;

import com.inspur.edp.common.expr.exception.ExpressException;
import com.inspur.edp.common.expr.spi.ArrayAccessVisitor;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/9/27 14:00
 */
public class ArrayAccessFactory {

    private final static Object lockObject = new Object();

    private static Map<String, ArrayAccessVisitor> PROVIDER_MAP;

    public static ArrayAccessVisitor getSpi(String type) {
        if (PROVIDER_MAP == null) {
            synchronized (lockObject) {
                if (PROVIDER_MAP == null) {
                    ConcurrentHashMap<String, ArrayAccessVisitor> temp = new ConcurrentHashMap<>();
                    //固定的几个
                    temp.put(DefaultArrayAccessVisitor.KIND, new DefaultArrayAccessVisitor());
                    temp.put(ArrayListAccessVisitor.KIND, new ArrayListAccessVisitor());
                    JsonArrayAccessVisitor jsonArrayAccessVisitor = new JsonArrayAccessVisitor();
                    temp.put(jsonArrayAccessVisitor.getKind(), jsonArrayAccessVisitor);
                    PROVIDER_MAP = temp;
                }
            }
        }
        ArrayAccessVisitor genSpi = PROVIDER_MAP.get(type);
        if (genSpi != null) {
            return genSpi;
        } else {
            throw new ExpressException("not find array access provider " + type);
        }
    }
}
